package analyse;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Vector;

public class AktienVerwaltung {
	
	private Kurs kurs;
	private final String AAPL = "apple.csv";
	private final String GOOG = "google.csv";
	private final String MSFT = "microsoft.csv";
	private final String RIMM = "rimm.csv";
	private final String YHOO = "yahoo.csv";
	private final String IXIC = "nasdaq.csv";
	
	public Kurs getKurs() {
		return kurs;
	}

	public void setKurs(Kurs kurs) {
		this.kurs = kurs;
	}

	public Vector<Kurs> leseDaten(String filename) throws IOException, FileNotFoundException {

		Vector<Kurs> kursList = new Vector<Kurs>();
		if ( filename.equals(AAPL) || filename.equals(GOOG) || filename.equals(MSFT) || filename.equals(RIMM) || filename.equals(YHOO) || filename.equals(IXIC) ) {

			String zeile = "";

			if(zeile != null) {

				// daten einlesen
				FileReader einlesen = new FileReader(filename);
				
				@SuppressWarnings("resource")
				BufferedReader br = new BufferedReader(einlesen);

				SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");

				String []attribute =null;
				Kurs tmp = null;

				int row = 0;

				while( (zeile = br.readLine()) != null)
				{

					if(row > 0){
						attribute = zeile.split(";");

						int volumen = Integer.valueOf(attribute[1]);
						double schlussKurs = Double.valueOf(attribute[2]);

						try {

							tmp = new Kurs(dateFormat.parse(attribute[0]), volumen, schlussKurs);
							kursList.add(tmp);

						} catch (ParseException e) {
							
							System.out.println("Datum, Integer, Double");
							e.printStackTrace();
						}

					} else {
						row = row + 1;
					  }
				}
			}
		}
		return kursList;
	}
}
